package com.jy.framework.utils.os;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * @Author: JuiceYan
 * @Description: 环境条件
 * @Class: ConditionLinux
 * @Date: 15:53 2018/12/11
 */
public class ConditionOS {

    private static final String WINDOWS = "windows";
    private static final String LINUX = "linux";

    /**
     * @Author: JuiceYan
     * @Description: Linux 环境条件
     * @Class: ConditionLinux
     * @Date: 15:53 2018/12/11
     */
    public static class ConditionLinux implements Condition {
        @Override
        public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
            return matchesOs(conditionContext, LINUX);
        }
    }

    private static boolean matchesOs(ConditionContext conditionContext, String os) {
        String osName = conditionContext.getEnvironment().getProperty("os.name");
        return StringUtils.containsIgnoreCase(osName, os);
    }

    /**
     * @Author: JuiceYan
     * @Description: Windows环境条件
     * @Class: ConditionWindows
     * @Date: 15:53 2018/12/11
     */
    public static class ConditionWindows implements Condition {
        @Override
        public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
            return matchesOs(conditionContext, WINDOWS);
        }
    }
}
